
const Mock = require("mockjs")
const Router=require("koa-router");
const router=new Router({
    prefix:"/api"
});

router.get("/users",async function(ctx){
    ctx.body=Mock.mock({
        state:true,
        msg:"数据加载成功",
        "users|2": [
            {
                No: "@id",
                name: "@cname",
                "sex|1": ["男", "女", "未知"],
                tel: /^1\d{10}$/,
                img: "https://api.ixiaowai.cn/mcapi/mcapi.php?_=@datetime('T')",
            }
        ]
    })
});

router.get("/params",async function(ctx){
    ctx.body = ctx.query;
});

router.post("/params",async function(ctx){
    ctx.body = ctx.request.body;
});


router.get("/login",async function(ctx){
    let user={
        username:"admin",
        password:"123456"
    };
    // token.sign(需要加密的数据对象, 加密秘钥, token的相关配置-有效期);
    // let tokenStr=token.sign(user,"itany secret",{expiresIn: '1h'});
    let tokenStr=ctx.token.sign(user,ctx.token.secret,{expiresIn: '1h'});
    ctx.body={
        state:true,
        msg:"登录成功",
        token:"Bearer "+tokenStr
    }

});
exports.apiRouter=router;